﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

namespace LunwenYun.Infrastructure.Entities.Repository
{
    [Table("T_Purpose")]
    public class T_Purpose : IEntity<T_Purpose>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public int PeriodicalId { get; set; }

        public int ArticleId { get; set; }

        public DateTime CreateTime { get; set; }


        public override T_Purpose Find(string key)
        {
            int id = Int32.Parse(key);
            return Find(c => c.Id == id);
        }

        public override T_Purpose Find(System.Linq.Expressions.Expression<Func<T_Purpose, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Purpose.FirstOrDefault(where);
                if (item != null)
                {
                    this.ArticleId = item.ArticleId;
                    this.CreateTime = item.CreateTime;
                    this.Id = item.Id;
                    this.PeriodicalId = item.PeriodicalId;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_Purpose, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Purpose.Any(where);
            }
        }
    }
}
